package com.kantboot.functional.common.web.controller;

import com.kantboot.functional.common.service.ICommonKantbootRsaService;
import com.kantboot.util.common.result.RestResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/functional-common-web/kantbootRsa")
public class CommonKantbootRsaController {

    @Resource
    private ICommonKantbootRsaService service;

    /**
     * 生成公钥
     * @return 公钥
     */
    @PostMapping("/generatePublicKey")
    public RestResult<String> generatePublicKey() {
        return RestResult.success(service.generatePublicKey(),"getSuccess","获取成功");
    }

    /**
     * 按照数量生成公钥（不要轻易暴露此接口）
     * @param num 数量
     */
    @PostMapping("/generatePublicKeyByNum")
    public RestResult<List<String>> generatePublicKeyByNum(@RequestParam("num") Integer num) {
        return RestResult.success(service.generatePublicKeyByNum(num),"getSuccess","获取成功");
    }

    /**
     * 生成批量公钥
     */
    @PostMapping("/generatePublicKeyBatch")
    public RestResult<List<String>> generatePublicKeyBatch() {
        return RestResult.success(service.generatePublicKeyBatch(),"getSuccess","获取成功");
    }


    /**
     * 根据公钥获取私钥 （不要轻易暴露此接口，否则会导致私钥泄露）
     * @param publicKey 公钥
     * @return 私钥
     */
    @PostMapping("/getPrivateKeyByPublicKey")
    public RestResult<String> getPrivateKeyByPublicKey(@RequestParam("publicKey") String publicKey) {
        return RestResult.success(service.getPrivateKeyByPublicKey(publicKey),"getSuccess","获取成功");
    }

    /**
     * 加密（不要轻易暴露此接口，加密时尽量在前端进行）
     * @param content 内容
     * @param publicKey 公钥
     * @return 加密后的内容
     */
    @PostMapping("/encrypt")
    public RestResult<String> encrypt(
            @RequestParam("content") String content,
            @RequestParam("publicKey") String publicKey) {
        return RestResult.success(service.encrypt(content,publicKey),"getSuccess","获取成功");
    }

    /**
     * 解密（不要轻易暴露此接口，否则会导致数据泄露）
     * @param content 内容
     * @return 解密后的内容
     */
    @PostMapping("/decrypt")
    public RestResult<String> decrypt(@RequestParam("content") String content) {
        return RestResult.success(service.decrypt(content),"getSuccess","获取成功");
    }


}
